<?php

  include_once( '../library/config.php' );
  include_once( LANGUAGE.'language.php' );
  include_once( APPLICATION.'User.class.php' );
  
  $_page = 'index';
  $_pname = 'Inici';
  
  //Login try
  if( isset( $_POST['email'] ) && isset( $_POST['passwd'] ) )
  {
    $user = User::login( $_POST['email'], $_POST['passwd'] );
  }
  //Logout
  elseif( isset( $_GET['act'] ) && ( $_GET['act'] == 'logout' ) )
  {
    unset( $_SESSION['email'] );
  }
  //Just access front page
  elseif( User::checkLogged() )
  {
    $user = User::findByEmail( $_SESSION['email'] );
  }
  
  include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/header.tmp.php' );
  
  //Invalid login
  if( empty( $user ) && isset( $_POST['email']) && isset( $_POST['passwd'] ) )
  {
    $msgko = 'Dades d\'accés incorrectes!';
    include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/invalid_login.tmp.php' );
  }
  //No login data
  elseif( empty( $user ) && !isset( $_POST['email'] ) )
  {
  	include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/login.tmp.php' );
  }
  //Logged in
  elseif( $user )
  {
  	include_once( APPLICATION.'Karma.class.php' );
  	include_once( APPLICATION.'Category.class.php' );
  	
  	$points = Karma::findPoints( $user->getId_user() );
  	$categories = Category::findVisible();
  	
  	include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/index_logged.tmp.php' );
  	include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/footer_logged.tmp.php' );
  }
  
  include_once( PUBLIC_TEMPLATES.$_SESSION['langShort'].'/footer.tmp.php' );
?>